<?php include THEME_PATH . '/Sidebar/Article_list_sidebar.php'; ?>
    <div class="tm-content">

        <ul class="am-list">
            <li class="am-padding-xs am-padding-left">
                <ol class="am-breadcrumb am-margin-0 am-padding-0">
                    <li><a href="/">首页</a></li>
                    <li>
                        <a href="<?= $label->url('Master-Index-index', ['type' => $timelog_type]); ?>"><?= $type_name; ?></a>
                    </li>
                </ol>
            </li>

            <li class="am-padding tm-remove-border-bottom">
                <h1 class="am-article-title am-margin-0 display-tm-title"><?= $timelog_title; ?></h1>

                <?php if ($_SESSION['user']['user_id'] == $user_id || $_SESSION['user']['user_group_id'] == 1): ?>
                    <!--修改标题、类型或者删除文章-->
                    <div class="am-form-inline am-padding-bottom-sm update-title-form am-hide">
                        <form action="<?= $label->url('Master-Article-action') ?>" class="ajax-submit" method="POST">
                            <input type="hidden" name="method" value="PUT">
                            <input type="hidden" name="id" value="<?= $timelog_id; ?>">

                            <div class="am-form-group" style="width:40%">
                                <input type="text" name="title" value="<?= $timelog_title; ?>" class="am-form-field" placeholder="标题" style="width:100%;padding:0.5rem">
                            </div>

                            <?php if ($_SESSION['user']['user_group_id'] == 1): ?>
                                <div class="am-form-group">
                                    <select name="timelog_type" data-am-selected="{maxHeight: 200, btnSize: 'sm'}">
                                        <option value="">请选择</option>
                                        <?php foreach ($type as $key => $value) : ?>
                                            <option value="<?= $value['type_id']; ?>" <?= $timelog_type == $value['type_id'] ? 'selected="selected"' : '' ?> ><?= $value['type_name']; ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            <?php endif; ?>

                            <button type="submit" class="am-btn am-btn-default" style="padding:0.51rem">更新标题</button>

                            <?php if ($_SESSION['user']['user_group_id'] == 1): ?>
                                <a href="<?= $label->url("Master-Article-Action", ['id' => $timelog_id, 'method' => 'DELETE']); ?>" class="am-btn am-btn-danger ajax-click ajax-delete" msg="确定删除吗？将无法恢复的！" style="padding:0.51rem">删除</a>
                            <?php endif; ?>
                        </form>
                    </div>
                    <!--修改标题、类型或者删除文章-->
                <?php endif; ?>

                <?php if (time() - $timelog_updatetime > 2592000): ?>
                    <div class="am-alert am-alert-secondary am-text-xs am-margin-bottom-0">
                        <p><i class="am-icon-exclamation-triangle"></i> 日志已超过30天没有更新，信息可能已经过时。</p>
                    </div>
                <?php endif; ?>
            </li>
            <?php foreach ($content as $key => $value) : ?>
                <li class="am-padding am-nbfc">
                    <article class="am-article">
                        <div class="am-article-hd">
                            <p class="am-article-meta">
                                <?php if ($key == 0): ?>
                                    <a href="<?= $label->url('Master-Article-user', ['id' => $value['user_id']]); ?>"><img src="<?= $value['user_head']; ?>" width="15" height="15"><span class="am-text-middle"> <?= $value['user_name']; ?></span></a>
                                <?php endif; ?>

                                <time class="am-text-middle" datetime="<?= date('Y-m-d H:i', $value['timelog_content_createtime']); ?>" title="<?= date('Y-m-d H:i', $value['timelog_content_createtime']); ?>">发表于 <?= $label->timing($value['timelog_content_createtime']); ?></time>
                                <?php if (!empty($value['timelog_content_updatetime'])): ?>
                                    <time class="am-text-middle" datetime="<?= date('Y-m-d H:i', $value['timelog_content_updatetime']); ?>" title="<?= date('Y-m-d H:i', $timelog_updatetime); ?>">最后更新 <?= $label->timing($value['timelog_content_updatetime']); ?></time>
                                <?php endif; ?>
                                <?php if ($_SESSION['user']['user_id'] == $user_id): ?>
                                    <a href="javascript:;" id="update-button_<?= $value['timelog_content_id'] ?>" data="<?= $value['timelog_content_id'] ?>" class="am-hide am-badge am-badge-primary update-button am-text-middle">更新</a>
                                <?php endif; ?>
                            </p>
                        </div>

                        <div class="am-article-bd <?= $_SESSION['user']['user_id'] == $user_id ? 'tm-article' : ''; ?>" data="<?= $value['timelog_content_id'] ?>">
                            <?php if ($_SESSION['user']['user_id'] == $user_id): ?>
                                <!--修改内容-->
                                <form id="submit_<?= $value['timelog_content_id'] ?>" class="ajax-submit" action="<?= $label->url('Master-Article-updateContent', ['id' => $value['timelog_content_id']]); ?>" method="POST">
                                    <input type="hidden" name="method" value="PUT">
                                    <script id="content_<?= $value['timelog_content_id'] ?>" type="text/plain" style="height:250px;"><?= htmlspecialchars_decode($value['timelog_content']); ?></script>
                                </form>
                                <!--修改内容-->
                            <?php endif; ?>
                            <div class="content_html">
                                <?= html_entity_decode($value['timelog_content']); ?>
                            </div>
                        </div>
                    </article>
                </li>
            <?php endforeach; ?>

            <?php if ($_SESSION['user']['user_id'] == $user_id): ?>
                <!--追加内容-->
                <form action="<?= $label->url("Master-Article-addContent", ['id' => $timelog_id]); ?>" class="ajax-submit" method="POST">
                    <li class="am-padding-xs ">
                        追加内容
                    </li>
                    <li class="">
                        <script id="editor" type="text/plain" style="height:250px;"></script>
                    </li>
                    <li class="am-padding-xs am-text-center">
                        <button class="am-btn  am-btn-xs am-btn-primary">添加</button>
                    </li>
                    <script>
                        var ue = UE.getEditor('editor', {
                            textarea: 'content'
                        });
                    </script>
                </form>
            <?php endif; ?>
        </ul>

        <?php include 'Article_comment.php'; ?>

    </div>
<?php if ($_SESSION['user']['user_id'] == $user_id || $_SESSION['user']['user_group_id'] == 1): ?>
    <script type="text/javascript">
        $(function () {
            /**
             * 记录已经生成的编辑器
             */
            var editor = {}
            /**
             * 任意点击解除编辑器
             * 否则将导致绑定body事件误触发
             */
            var currentUse = false;
            $(".tm-article").on("dblclick", function () {
                currentUse = true;
                var data = $(this).attr("data");
                //记录启用过和初始化编辑器
                if (!editor[data]) {
                    editor[data] = UE.getEditor('content_' + data, {textarea: 'content'});
                } else {
                    editor[data].setShow()
                }

                //移除所有隐藏得元素
                $(".update-button, .history-button, .delete-content-button").addClass("am-hide");
                $(".content_html").removeClass("am-hide");
                //隐藏当前内容
                $(this).children(".content_html").addClass("am-hide");
                $("#update-button_" + data + ", #history-button_" + data + ", #delete-content-button_" + data).removeClass("am-hide");
                for (var key in editor) {
                    if (key != data) {
                        editor[key].setHide();
                    }
                }
            })


            /**
             * 更新标题
             */
            $(".display-tm-title").on("dblclick", function () {
                currentUse = true;
                $(".update-title-form").removeClass("am-hide");
                $(this).addClass("am-hide");
            })

            /**
             * 解除编辑功能
             */
            $("body").on("dblclick", function () {
                if (editor && currentUse == false) {
                    $(".content_html, .display-tm-title").removeClass("am-hide");
                    for (var key in editor) {
                        $(".update-button, .history-button, .delete-content-button").addClass("am-hide");
                        editor[key].setHide();
                    }
                    $(".update-title-form").addClass("am-hide");
                }
                if (currentUse == true) {
                    currentUse = false
                }
            })

            /**
             * 提交更新内容
             */
            $(".update-button").on("click", function () {
                var id = $(this).attr("data");
                $("#submit_" + id).submit();
                return false;
            })
        })
    </script>
<?php endif; ?>